import axios from 'axios'
import getMethods from './ajaxMethods/getMethods.js'
import postMethods from './ajaxMethods/postMethods.js'
import deleteMethods from './ajaxMethods/deleteMethods.js'

var noop = function () {}
var Options = {
    checkToken: true,
    baseUrl: '',
    apiList: null
};
var instance = function () {
  return {
    init: function (options) {
      Options =Object.assign({}, Options, options); 
      this.methods = {
        ...getMethods(Options),
        ...postMethods(Options),
        ...deleteMethods(Options),
        /**
         * 多个promise并行调用
         * @method all
         * @memberof GetData
         * @example
         * axios.all(arg1, arg2, arg3...)
         *   .then(axios.spread)
         */
        all: function () {
            return axios.all.apply(null, arguments);
        },
        spread: function () {
            return axios.spread.apply(null, arguments);
        },

        /**
         * 传入多个promise的值，当请求成功之后统一反馈，做逻辑处理
         * */
        allPromise: function(promise1,promise2){
            var userPromise = Array.prototype.slice.apply(arguments);
            var promiseObj = new Promise(function(resolve, reject){
                axios.all(userPromise).then(axios.spread(function () {
                    resolve(arguments);
                }),function(){
                    reject(arguments);
                });
            });
            return promiseObj;
        },

        /**
         * 自由配置参数
         * @method axios
         * @memberof GetData
         * @param configObj 发送ajax 的配置参数
         * @returns {promise} 返回promise对象
         * @example
         * axios({
         *   method: 'post',
         *   url: '/user/12345',
         *   data: {
         *     firstName: 'Fred',
         *     lastName: 'Flintstone'
         *   }
         * });
         */
        axios: function (configObj) {
            return axios(configObj);
        }
      }
    },
    methods: noop
  }
}

export default new instance();
